import { Body, Controller, Post, Get, HttpCode, HttpStatus } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
import { AuthService } from './auth.service';
import { LoginDto } from './dto/login.dto';
import { AuthResponse, ProfileResponse } from './dto/auth.response';
import { Auth } from './decorators/auth.decorator';
import { User } from './decorators/user.decorator';

@ApiTags('认证')
@Controller('auth')
export class AuthController {
  constructor(private authService: AuthService) {}

  @Post('login')
  @HttpCode(HttpStatus.OK)
  @ApiOperation({ summary: '用户登录' })
  @ApiResponse({ type: AuthResponse, status: 200, description: '登录成功' })
  @ApiResponse({ status: 401, description: '用户名或密码错误' })
  async login(@Body() loginDto: LoginDto): Promise<AuthResponse> {
    return this.authService.login(loginDto);
  }

  @Get('profile')
  @Auth()
  @ApiOperation({ summary: '获取用户信息' })
  @ApiResponse({ type: ProfileResponse, status: 200, description: '获取成功' })
  getProfile(@User() user): ProfileResponse {
    return {
      id: user.id,
      username: user.username,
      email: user.email
    };
  }

  @Post('logout')
  @Auth()
  @HttpCode(HttpStatus.OK)
  @ApiOperation({ summary: '用户登出' })
  @ApiResponse({ status: 200, description: '登出成功' })
  logout() {
    return { message: '退出成功' };
  }
}
